/**
 * 
 */
package com.ckex.learn.lucene.search;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;

import com.ckex.learn.lucene.index.IndexService;

/**
 * @author ckex created 2014-1-17 下午3:13:02
 * @explain -
 */
public class SearchService {
    protected static Logger logger   = Logger.getLogger(SearchService.class);

    private static Analyzer analyzer = new IKAnalyzer(Boolean.TRUE);

    public static List<Long> search(String field, String value) {

        List<Long> tradeIds = new ArrayList<Long>();

        File indexFile = new File(IndexService.indexPath);
        try {
            IndexReader reader = DirectoryReader.open(FSDirectory.open(indexFile));
            IndexSearcher searcher = new IndexSearcher(reader);

            Query query = new TermQuery(new Term(field, value));
            TopDocs td = searcher.search(query, Integer.MAX_VALUE);//获取最高得分命中
            for (ScoreDoc doc : td.scoreDocs) {
                Document d = searcher.doc(doc.doc);
                tradeIds.add(Long.parseLong(d.get("id")));
            }
            reader.close();//关闭读取流
        } catch (IOException e) {
            e.printStackTrace();
        }

        return tradeIds;
    }

    /***
     * 简单查询的方法
     * 
     * @param indexReadPath 读取的索引路径
     * @param filed 查询的字段类型
     * @param searchText查询的文本
     */
    public static List<Long> simpleSearch(String field, String value) {

        List<Long> tradeIds = new ArrayList<Long>();
        File indexFile = new File(IndexService.indexPath);
        IndexReader reader;
        try {
            reader = DirectoryReader.open(FSDirectory.open(indexFile));
            IndexSearcher searcher = new IndexSearcher(reader);
            QueryParser parser = new QueryParser(Version.LUCENE_44, field, analyzer);
            Query query = parser.parse(value);
            TopDocs td = searcher.search(query, Integer.MAX_VALUE);//获取匹配上元素的一个docid
            ScoreDoc[] sd = td.scoreDocs;//加载所有的Documnet文档
            for (ScoreDoc scoreDoc : sd) {
                int num = scoreDoc.doc;//获取每一个文档编号
                Document d = searcher.doc(num);//获取文档
                tradeIds.add(Long.parseLong(d.get("id")));
            }
            reader.close();//关闭资源
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return tradeIds;
    }

    public static List<Long> search(String[] fields, String[] values, BooleanClause.Occur[] clauses) {

        List<Long> tradeIds = new ArrayList<Long>();
        File indexFile = new File(IndexService.indexPath);
        IndexReader reader;
        try {
            reader = DirectoryReader.open(FSDirectory.open(indexFile));
            IndexSearcher searcher = new IndexSearcher(reader);

            Query query = MultiFieldQueryParser.parse(Version.LUCENE_44, values, fields, clauses,
                    analyzer);
            /* 开始搜索 */
            TopDocs td = searcher.search(query, Integer.MAX_VALUE);//获取匹配上元素的一个docid
            ScoreDoc[] sd = td.scoreDocs;//加载所有的Documnet文档
            for (ScoreDoc scoreDoc : sd) {
                int num = scoreDoc.doc;//获取每一个文档编号
                Document d = searcher.doc(num);//获取文档
                tradeIds.add(Long.parseLong(d.get("id")));
            }
            reader.close();//关闭资源
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return tradeIds;
    }
}
